package com.ws.app.xls;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.ws.app.entity.Expenditure;
import com.ws.base.common.WSAppLogger;
import com.ws.base.common.util.WSUtil;
import com.ws.base.common.xls.ExcelSupport;
import com.ws.base.exception.WSRuntimeException;

public class ExpenditureXls extends ExcelSupport<Expenditure> {

    private final WSAppLogger logger = WSAppLogger
            .getWSAppLogger(ExpenditureXls.class);

    private final String[] weekDay = { "", "星期日", "星期一", "星期二", "星期三", "星期四",
            "星期五", "星期六" };

    private final String[] property = { "日", "周", "值", "描述" };

    private Date xlsDate;

    private int year;

    /*
     * public ExpenditureXls() { super.setProperty(property); }
     */

    public ExpenditureXls(String filePath, Date xdate) {
        super.setProperty(property);
        super.setFilePath(filePath);
        this.xlsDate = xdate;
        String filename = filePath.substring(filePath.length() - 8);
        setYear(Integer.valueOf(filename.replace(".xls", "")));
    }

    public ExpenditureXls(String filePath) {
        super.setProperty(property);
        super.setFilePath(filePath);
        String filename = filePath.substring(filePath.length() - 8);
        setYear(Integer.valueOf(filename.replace(".xls", "")));
    }

    public void createExpenditure() {
        createExpenditure(new Date());
    }

    public void createExpenditure(Date args) {
        super.setRecords(getTemplate(args));
        super.createXls();
    }

    public List<String[]> getTemplate(Date args) {
        List<String[]> records = new ArrayList<String[]>();
        Date date = args;
        List<Date> dates = WSUtil.getAllTheDateOftheMonth(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String sheetName = calendar.get(Calendar.MONTH) + 1 + "月";
        super.setSheetName(sheetName);

        for (Date d : dates) {
            calendar.setTime(d);
            String[] strs = { calendar.get(Calendar.DATE) + "",
                    weekDay[calendar.get(Calendar.DAY_OF_WEEK)] };
            records.add(strs);
        }
        return records;
    }

    public List<Expenditure> read() {
        // super.setFilePath(filePath);
        return super.readXls(getESheetName());
    }

    public void updateExpenditure(List<Expenditure> expenditures) {
        super.setSheetName(getESheetName());
        updateXls(expenditures);
    }

    @Override
    protected void update(WritableWorkbook book, List<Expenditure> list) {
        WritableSheet writableSheet = book.getSheet(getSheetName());

        if (null == writableSheet) {
            writableSheet = book.createSheet(getSheetName(),
                    book.getNumberOfSheets());
            super.setRecords(getTemplate(getXlsDate()));
            super.loadXlsTemplate(writableSheet);
        }

        Calendar calendar = Calendar.getInstance();
        try {
            for (Expenditure e : list) {
                calendar.setTime(e.getDate());
                Label l1 = new Label(2, calendar.get(Calendar.DATE),
                        e.getMoney() + "");
                Label l2 = new Label(3, calendar.get(Calendar.DATE),
                        e.getOther());
                writableSheet.addCell(l1);
                writableSheet.addCell(l2);
            }
        } catch (RowsExceededException e) {
            logger.error(e, "update ExpenditureXls error");
        } catch (WriteException e) {
            logger.error(e, "update ExpenditureXls error");
        }

    }

    @Override
    protected Expenditure readLine(Sheet sheet, int lineNum, String sheetName) {
        Expenditure expenditure = new Expenditure();

        if (lineNum == 0) {
            expenditure = null;
        } else {
            Calendar calendar = Calendar.getInstance();
            sheetName = sheetName.replace("月", "");
            Cell[] cells = sheet.getRow(lineNum);
            int date = Integer.valueOf(cells[0].getContents());
            int month = Integer.valueOf(sheetName) - 1;
            calendar.set(getYear(), month, date);
            expenditure.setMoney(Integer
                    .valueOf(null == getCellVal(cells, 2) ? "0" : getCellVal(
                            cells, 2)));
            expenditure.setOther(getCellVal(cells, 3));
            expenditure.setDate(calendar.getTime());
        }

        return expenditure;
    }

    public Date getXlsDate() {
        return xlsDate;
    }

    public void setXlsDate(Date xlsDate) {
        this.xlsDate = xlsDate;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getESheetName() {

        if (null == getXlsDate()) {
            throw new WSRuntimeException("XlsDate is null");
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getXlsDate());
        return calendar.get(Calendar.MONTH) + 1 + "月";
    }

}
